package com.crlui.core.state
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	public class SceneMachine extends StateMachine
	{
		private var container:Sprite;
		public function SceneMachine(container:Sprite)
		{
			this.container=container;
		}
		
		
		override public function set state(name:String):void{
			
			var state:IState=states[name];
			
			if(currentState){
				currentState.end();
				endEffect(currentState);
			}
			
			
			currentState=state;
			
			if(currentState){
				if(currentState.initialized==false)currentState.initialize();
				
				startEffect(currentState);
				currentState.start();
			}
			
		}
		
		
		protected function endEffect(state:IState):void{
			container.removeChild(state as DisplayObject);
		}
		
		protected function startEffect(state:IState):void{
			container.addChild(state as DisplayObject);
		}
		
	}
}